package io.terminus.spzx.model.vo.common;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * @author lichunlong
 * @desc 响应结果实体类
 */
@Data
@Schema(description = "响应结果实体类")
public class Result<T> {

    /**
     * 返回码
     */
    @Schema(description = "业务状态码")
    private Integer code;

    /**
     * 返回消息
     */
    @Schema(description = "响应消息")
    private String message;

    /**
     * 返回数据
     */
    @Schema(description = "业务数据")
    private T data;

    /**
     * 私有化构造
     */
    private Result() {
    }

    /**
     * 返回数据
     *
     * @param body    业务数据
     * @param code    业务状态码
     * @param message 响应消息
     * @param <T>     泛型类
     * @return 泛型数据
     */
    public static <T> Result<T> build(T body, Integer code, String message) {
        Result<T> result = new Result<>();
        result.setData(body);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * 通过枚举构造Result对象
     *
     * @param body           业务数据
     * @param resultCodeEnum 操作结果状态
     * @param <T>            泛型类
     * @return 泛型数据
     */
    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
        return build(body, resultCodeEnum.getCode(), resultCodeEnum.getMessage());
    }

}
